import codecs
import os
import shutil

import config_setting


def start(index_folder_path, output_folder_path):
    if not os.path.isdir(output_folder_path):
        os.mkdir(output_folder_path)
    count = 0
    for dirpath, dirnames, filenames in os.walk(index_folder_path):
        for filename in filenames:
            relative_path = dirpath.replace(index_folder_path, "").lstrip("\\").lstrip("/")  # strip the leading path
            if filename.endswith(".html"):
                path = os.path.join(dirpath, filename).replace("\\", "/")
                with codecs.open(path, mode='r', encoding='utf-8') as f:
                    content_head = f.read()
                    if "<video" in content_head:
                        contains_video = True
                    else:
                        contains_video = False

                if contains_video:
                    print(f"copying {os.path.join(dirpath, filename)}")
                    shutil.copy(os.path.join(dirpath, filename), output_folder_path)

                count += 1
                print(f"{count} checking file: {path}")
                # count += 1
                # url = getUrl_from_content(content_head).strip()
                # url_list.append(url)


if __name__ == "__main__":
    # batch_name = "20240101"
    archive_index_folder_path = f"E:/03.WebArchivesInbox/inbox/{config_setting.batch_name}"
    output_folder_path = f"E:/03.WebArchivesInboxVideo/___todo/{config_setting.batch_name}"
    start(archive_index_folder_path, output_folder_path)

    # if not os.path.isdir(output_folder_path):
    #     os.mkdir(output_folder_path)
    # count = 0
    # for dirpath, dirnames, filenames in os.walk(archive_index_folder_path):
    #     for filename in filenames:
    #         relative_path = dirpath.replace(archive_index_folder_path, "").lstrip("\\").lstrip("/")  # strip the leading path
    #         if filename.endswith(".html"):
    #             path = os.path.join(dirpath, filename).replace("\\", "/")
    #             with codecs.open(path, mode='r', encoding='utf-8') as f:
    #                 content_head = f.read()
    #                 if "<video" in content_head:
    #                     contains_video = True
    #                 else:
    #                     contains_video = False
    #
    #             if contains_video:
    #                 print(f"copying {os.path.join(dirpath, filename)}")
    #                 shutil.copy(os.path.join(dirpath, filename), output_folder_path)
    #
    #             count += 1
    #             print(f"{count} checking file: {path}")
    #             # count += 1
    #             # url = getUrl_from_content(content_head).strip()
    #             # url_list.append(url)
